package com.njit.WeStudy.control;

import com.njit.WeStudy.dao.*;
import com.njit.WeStudy.dataobject.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuruiqiao
 * @date 2020/1/28
 * @description 用户点击发帖的列表会转到对应的详情页
 */
@Controller
public class InvitationRequestController {

    @Resource
    private InvitationMapper invitationMapper;

    @Resource
    private StoreyMapper storeyMapper;

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private LikeMapper likeMapper;

    @Resource
    private StarMapper starMapper;

    @Resource
    private  SupportMapper supportMapper;

    @RequestMapping(path = "/users/**")
    public String response(HttpSession session, @RequestParam("invitationId") int invitationId, ModelMap modelMap){
        int userId;
        try{
            userId = ((User)session.getAttribute("user")).getUserId();
            //查询当前用户是否like
            Like like = likeMapper.queryMyselfLike(invitationId,userId);
            if(like!=null){
                modelMap.addAttribute("myLikeStatus",like.getIsLiked());
            }
            //查询当前用户是否star
            Star star = starMapper.queryMyselfStar(invitationId,"invitation",userId);
            if(star!=null){
                modelMap.addAttribute("myStarStatus",star.getIsStared());
            }
            //查询当前用户是否关注当前帖子的发出者
            Invitation invitationTemp = invitationMapper.selectByPrimaryKey(invitationId);
            Star userStar = starMapper.queryMyselfStar(invitationTemp.getUserId(),"user",userId);
            if(userStar!=null){
                modelMap.addAttribute("userStarStatus",userStar.getIsStared());
            }
            //查询当前用户是否comment
            List<Comment> myComments = commentMapper.QueryMySelfComments(invitationId,userId);
            if(myComments.size()!=0){
                modelMap.addAttribute("myComments",myComments);
            }
            //查询当前用户是否storey
            List<Storey> myStoreys = storeyMapper.queryMyselfStoreys(userId,invitationId);
            if(myStoreys.size()!=0){
                modelMap.addAttribute("myStoreys",myStoreys);
            }
        }catch (NullPointerException ne){
            System.out.println();
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            userId = ((User)session.getAttribute("user")).getUserId();
            List<Support> supportsOfInvitationId = supportMapper.mySupportIdsQueryByInvitationId(invitationId,userId);
            List<Integer> myStoreyIdsOfSupport= new ArrayList<>();
            List<Integer> myCommentIdsOfSupport= new ArrayList<>();
            for (Support support : supportsOfInvitationId) {
                if(support.getTargetType().equals("storey")){
                    myStoreyIdsOfSupport.add(support.getTargetId());
                }else {
                    myCommentIdsOfSupport.add(support.getTargetId());
                }
            }
            modelMap.addAttribute("myStoreyIdsOfSupport",myStoreyIdsOfSupport);
            modelMap.addAttribute("myCommentIdsOfSupport",myCommentIdsOfSupport);
            List<Comment> commentsOfInvitationId = commentMapper.myCommentIdsQueryByInvitationId(invitationId,userId);
            List<Integer>myStoreyIdsOfComment = new ArrayList<>();
            for (Comment comment : commentsOfInvitationId) {
                if(comment.getTargetType().equals("storey")){
                    myStoreyIdsOfComment.add(comment.getStoreyId());
                }
                myStoreyIdsOfComment.add(comment.getStoreyId());//属于storey的评论也算
            }
            modelMap.addAttribute("myStoreyIdsOfComment",myStoreyIdsOfComment);

        }catch (NullPointerException ne){
            System.out.println();
        }catch (Exception e){
            e.printStackTrace();
        }
        InvitationView invitationView = invitationMapper.selectViewByPrimaryKey(invitationId);
        modelMap.addAttribute("invitation",invitationView);
        List<StoreyView> storeyViews = storeyMapper.selectViewByInvitationId(invitationId);
        modelMap.addAttribute("storeyViews",storeyViews);
        List<CommentViewPlus> CommentViewPlusList = commentMapper.queryCommentViewPlusByInvitationId(invitationId);
        modelMap.addAttribute("comments",CommentViewPlusList);
        return invitationView.getHtmlUrl();
    }
}
